﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AMM.Model;
using AMM.Model.Finance;

namespace AMM.DataAccess.Finance {
    public class CostRepository : BaseRepository<Cost> {

        private int _pageSize;

        public CostRepository(int pageSize) {
            this._pageSize = pageSize;
        }

        public Cost FetchCostById(int costId) {
            using (var ctx = this.Context) {
                return ctx.Costs.Find(costId);
            }
        }

        public void DeleteMultipleCost(params int[] costIds) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        ctx.Costs.Where(x => costIds.Contains(x.CostId)).ToList().ForEach(x => {
                            ctx.Costs.Remove(x);
                        });
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }

        public List<Cost> FetchCostList() {
            using (var ctx = this.Context) {
                return ctx.Costs.ToList();
            }
        }

        public List<Cost> FetchCostList(CostSearchModel searchModel, int pageIndex) {
            using (var ctx = this.Context) {
                var query = ctx.Costs
                    .Include("Statement")
                    .Where(x => 1 == 1);

                if (searchModel.CostType.HasValue) {
                    query = query.Where(x => x.CostType == searchModel.CostType.Value);
                }
                if (!string.IsNullOrEmpty(searchModel.AccountNumber)) {
                    query = query.Where(x => x.AccountNumber.Contains(searchModel.AccountNumber));
                }
                if (searchModel.StatementId.HasValue) {
                    query = query.Where(x => x.StatementId == searchModel.StatementId.Value);
                }
                if (!string.IsNullOrEmpty(searchModel.Remarks)) {
                    query = query.Where(x => x.Remarks.Contains(searchModel.Remarks));
                }
                if (searchModel.CreateDateStart.HasValue) {
                    query = query.Where(x => x.CreatedDatetime >= searchModel.CreateDateStart.Value);
                }
                if (searchModel.CreateDateEnd.HasValue) {
                    query = query.Where(x => x.CreatedDatetime <= searchModel.CreateDateEnd.Value);
                }

                searchModel.Records = query.Count();
                pageIndex = (pageIndex - 1) * this._pageSize > searchModel.Records ? 1 : pageIndex;

                return query.OrderBy(x => x.CostId).Skip((pageIndex - 1) * this._pageSize).Take(this._pageSize).ToList();
            }
        }

    }
}
